﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ATSBLL;
using System.IO;
using System.Text;

public partial class AntDebInfo_DebReport_DebDataReport : System.Web.UI.Page
{
    private DataSet DebDataSet { get { return (DataSet)ViewState["MyView"]; } set { ViewState["MyView"] = value; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // 根据产品型号获取数据
    protected void ProTypeSerach_Click(object sender, EventArgs e)
    {
        string ProType = ProType_text.Text.Trim();
        if (ProType == "")
            return;

        DebDataSet = new DataSet();
        Report_DebInfo batch = new Report_DebInfo();

        DebDataSet = batch.GetDataByProType(ProType);
        if (DebDataSet.Tables[0].Rows.Count == 0)
            return;

        DateHandle();
        GridView1.DataSource = DebDataSet.Tables[0];
        GridView1.DataBind();

        //ExportDataGrid("application/ms-excel", "天线调试信息.xls");
    }

    // 根据天线MO号搜索数据
    protected void MOSerach_Click(object sender, EventArgs e)
    {
        string MOID = this.MO_text.Text.Trim();
        if (MOID == "")
            return;

        DebDataSet = new DataSet();
        Report_DebInfo batch = new Report_DebInfo();

        DebDataSet = batch.GetDataByMO(MOID);
        if (DebDataSet.Tables[0].Rows.Count == 0)
            return;

        DateHandle();
        GridView1.DataSource = DebDataSet.Tables[0];
        GridView1.DataBind();

        //ExportDataGrid("application/ms-excel", "天线调试信息.xls");
    }

    // 根据天线条码搜索数据
    protected void ProSNSerach_Click(object sender, EventArgs e)
    {
        DebDataSet = new DataSet();
        Report_DebInfo batch = new Report_DebInfo();

        string ProSN = this.Pro_text.Text;
        DebDataSet = batch.GetDataByProSN(ProSN);
        if (DebDataSet.Tables[0].Rows.Count == 0)
            return;

        DateHandle();
        GridView1.DataSource = DebDataSet.Tables[0];
        GridView1.DataBind();

       // ExportDataGrid("application/ms-excel", "天线调试信息.xls");
    }

    // 根据时间段得到调试数据
    protected void DateSerach_Click(object sender, EventArgs e)
    {
        DateTime dt1 = DateTime.Parse(this.S_Date.Text.Trim());
        DateTime dt2 = DateTime.Parse(this.E_Date.Text.Trim());

        DebDataSet = new DataSet();
        Report_DebInfo batch = new Report_DebInfo();

        DebDataSet = batch.GetDataByDate(dt1, dt2);
        if (DebDataSet.Tables[0].Rows.Count == 0)
            return;

        DateHandle();
        GridView1.DataSource = DebDataSet.Tables[0];
        GridView1.DataBind();

      //  ExportDataGrid("application/ms-excel", "天线调试信息.xls");
    }

    // 对按条件查询出来的数据集进行列名整理，将列值为空的全部去掉
    private void DateHandle()
    {
        DataTable TempTable;
        bool IsDelCol;               // 判断某列是否应该删除
        string ColName, ColValue;

        DebDataSet.Tables[0].Columns.Remove("ID");
        TempTable = DebDataSet.Tables[0];

        // 遍历所有列，如果某列的数据全部为空则删除该列
        for (int ColNum = 0; ColNum < TempTable.Columns.Count; ColNum++)
        {
            ColName = ColValue = "";
            IsDelCol = true;
            ColName = TempTable.Columns[ColNum].ColumnName;

            if (ColName.Contains("调试前"))
            {
                for (int RowNum = 0; RowNum < TempTable.Rows.Count; RowNum++)
                {
                    ColValue = TempTable.Rows[RowNum][ColNum].ToString();
                    if (!String.IsNullOrEmpty(ColValue))
                    {
                        IsDelCol = false;
                        break;
                    }
                }

                // 如果“*调试前”这列数据不为空，则不需要对后两列"*调试后"、"*调试方法"进行判断
                if (!IsDelCol)
                    ColNum = ColNum + 2;
            }
            else
                IsDelCol = false;

            // 如果“*调试前”这列数据全部为空，则将后面两列也全部删掉
            if (IsDelCol)
            {
                TempTable.Columns.RemoveAt(ColNum);
                TempTable.Columns.RemoveAt(ColNum);
                TempTable.Columns.RemoveAt(ColNum);

                ColNum--;
            }
        }

        DebDataSet.Tables.RemoveAt(0);
        DebDataSet.Tables.Add(TempTable);
    }

    protected void PrintReport_Click(object sender, EventArgs e)
    {
        ExportDataGrid("application/ms-excel", "天线调试信息.xls");
    }

    private void ExportDataGrid(string FileType, string FileName)
    {
        System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();

        dg.DataSource = DebDataSet;
        dg.DataBind();

        //定义文档、字符编码 
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.Charset = "UTF-8";
        Response.ContentEncoding = Encoding.Default;
        Response.ContentType = FileType;
        dg.EnableViewState = false;
        //定义一个输入流　　   
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        //目标数据绑定到输入流输出　  
        dg.RenderControl(hw);
        //GvContract 绑定datagrid,或其他支持obj.RenderControl()属性的控件　　   
        //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "info", tw.ToString(), false);  
        Response.Write(tw.ToString());
        Response.End();
        //sqlCon.Close();
    }
}
